package doa;

import java.util.ArrayList;
import java.util.List;
import models.Category;
import models.Product;
import org.hibernate.Session;
import org.hibernate.Transaction;
import service.HibernateUtil;

/**
 *
 * @author User
 */
public class CategoryDAO {

    public CategoryDAO() {
    }

    public static List<Category> getAllCategories() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Category> category_list = session.createCriteria(Category.class).list();
        tx.commit();

        return category_list;
    }

    public static Category getCategory(int Category_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Category category = (Category) session.get(Category.class, Category_id);
        tx.commit();
        return category;
    }

    public static ArrayList insertCategory(Category category) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        // product = getProductFromRequest(request);
        session.save(category);
        tx.commit();
        return validation_errors;
    }

    public static ArrayList editCategory(Category category) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.update(category);
        tx.commit();
        return validation_errors;
    }

    public static void deleteCategory(int Category_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Category category = (Category) session.load(Category.class, Category_id);
        session.delete(category);
        tx.commit();
    }
}
